package com.cowmooed.controller;

import com.cowmooed.dto.FileUrlItem;
import com.cowmooed.dto.FileUrlRequest;
import com.cowmooed.entity.Result;
import com.cowmooed.service.impl.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/upload")
public class FileUploadController {

    @Autowired
    private UploadService uploadService;

    /**
     * 批量上传文件URL到CDN
     * @param request 包含文件URL数组的请求
     * @return 处理结果
     */
    @PostMapping("/batch-urls")
    public Result batchUploadUrls(@RequestBody FileUrlRequest request) {
        try {
            if (request.getFiles() == null || request.getFiles().isEmpty()) {
                return Result.error("文件列表不能为空");
            }

            // 处理文件上传
            List<FileUrlItem> processedFiles = uploadService.batchUploadFromUrls(request.getFiles());

            return Result.success(processedFiles);

        } catch (Exception e) {
            System.err.println("批量上传失败: " + e.getMessage());
            return Result.error("批量上传失败");
        }
    }
}
